<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 交易模型
 */
class BillOrder extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    public static $recharge_withdraw_status = [0 => '审核中', 1 => '转账中', 2 => '成功'];

    protected static function init()
    {
    }

    /**
     * 记录交易
     * @param $uid int 转出者id
     * @param $to_uid int 转入者id
     * @param $num int 数量
     * @param $amount float 金额
     * @param $coin_id int 币种ID
     * @param $type int 类别ID
     * @param $type_str string 类型说明
     * @param $status int 状态 0：未付款 1：已付款 2：已收货 3：已完成 4：订单失败
     */
    public static function add_bill($uid, $to_uid = '', $tradeid = '', $num = 1, $amount = 1, $coin_id = 0, $type = 0, $type_str = '', $status = 0)
    {
        if (!is_numeric($type)) {
            $type_str = $type;
        }
        if ($type_str) {
            $type_id = LogType::get_type_id($type_str);
            if ($type_id) {
                $type = $type_id;
            }
        }
        if (!is_numeric($to_uid)) {
            $to_address = $to_uid;
            $to_uid = '';
        }
        $bill_id = model('BillOrder')->insertGetId(['uid' => $uid, 'to_uid' => $to_uid, 'to_address' => $to_address, 'tradeid' => $tradeid, 'num' => $num, 'amount' => $amount, 'coin_id' => $coin_id, 'type' => $type, 'type_str' => $type_str, 'status' => $status, 'createtime' => time(), 'updatetime' => time(), 'date_str' => date("Ymd")]);

        return $bill_id;
    }
}
